//
//  CommonFailureCallback.m
//  YiJuZhangShou
//
//  Created by 冷悦 on 16/3/4.
//  Copyright © 2016年 Uranus. All rights reserved.
//

#import "CommonFailureCallback.h"
#import "ErrorLogDataObject.h"
#import "AppContext.h"
#import "SyncTimer.h"
#import "CoreDataUtil.h"

@implementation CommonFailureCallback

#pragma mark - Private

- (void)manager:(NetworkingBaseManager *)manager callAPIDidError:(NSDictionary *)responseParams{
    NSInteger userId = [AppContext sharedInstance].userId;
    long long timeStamp = [SyncTimer sharedInstance].systemTimeMillis;
    NSInteger code = [[responseParams objectForKey:@"stateCode"] integerValue];
    NSString *message = [responseParams objectForKey:@"stateMessage"];
    NSString *api = manager.apiName;
    ErrorLogDataObject *errorObject = [[ErrorLogDataObject alloc]initWithUserId:userId
                                                                   code:code
                                                                message:message
                                                                    api:api
                                                           andTimestamp:timeStamp];
/*#pragma message("CoreData Normal Error")
    DDLogVerbose(@"此处应该记录在coredata中 %@", errorObject);*/
    [[CoreDataUtil sharedInstance].errorLogs addObject:errorObject];
    
//    [[CoreDataUtil sharedInstance] saveContext];
}

- (void)manager:(NetworkingBaseManager *)manager callAPIDidFailed:(NSError *)error andTask:(NSURLSessionDataTask *)task{
    NSInteger userId = [AppContext sharedInstance].userId;
    long long timeStamp = [SyncTimer sharedInstance].systemTimeMillis;
    NSInteger code = ((NSHTTPURLResponse *)task.response).statusCode;
    NSString *message = [error.userInfo objectForKey:@"NSLocalizedDescription"];
    NSString *api = manager.apiName;
    ErrorLogDataObject *errorObject = [[ErrorLogDataObject alloc]initWithUserId:userId
                                                                   code:code
                                                                message:message
                                                                    api:api
                                                           andTimestamp:timeStamp];
/*#pragma message("CoreData Failure Error")
    DDLogVerbose(@"此处应该记录在coredata中 %@", errorObject);*/
    [[CoreDataUtil sharedInstance].errorLogs addObject:errorObject];
    
}

@end
